package com.rocsea.robot.template.impl;

import com.rocsea.robot.config.build.TemplateInfo;
import com.rocsea.robot.factory.KettleTemplateFactory;
import com.rocsea.robot.model.enums.KettleBuildStrategyEnum;
import com.rocsea.robot.model.bo.KpBuildInfo;
import org.springframework.stereotype.Component;
import java.text.ParseException;

/**
 * 分页更新
 * @Author RocSea
 * @Date 2022/12/7
 */
@Component
public class PagingUpdateTemplate extends FirstBatchTemplate {
    @Override
    public TemplateInfo getTemplateInfo() {
        return getTemplateConfig().getPagingUpdate();
    }

    @Override
    public String processKettlePack() {
        String jobParamTemplate = "{\"TABLENAME\":\"%s\",\"TASKNAME\":\"%s\",\"compensation_type\":\"2\"" +
                ",\"page_size\":\"5000\",\"commit_size\":\"1000\",\"outTableName\":\"%s\"}";
        KpBuildInfo kpBuildInfo = createKpBuildInfo(getBuildConfig().getOutDatabase());
        String jobParam = String.format(jobParamTemplate, kpBuildInfo.getTableName(),
                kpBuildInfo.getTaskName(), kpBuildInfo.getTargetTableName());
        kpBuildInfo.setJobParam(jobParam);
        return getKettlePackService().buildKettlePack(kpBuildInfo);
    }

    @Override
    public String processKettleTaskConfig() throws ParseException {
        return getKettleTaskConfigService().buildKettleTaskConfig(false);
    }

    @Override
    public void afterPropertiesSet() {
        KettleTemplateFactory.register(KettleBuildStrategyEnum.PAGING_UPDATE.getText(), this);
    }

}
